package com.raja.servingStation.controller.claim;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.common.exception.BusinessRuntimeException;
import com.raja.servingStation.common.util.AssertUtils;
import com.raja.servingStation.common.util.RequestUtil;
import com.raja.servingStation.common.util.ResponseUtil;
import com.raja.servingStation.common.util.ReturnCodes;
import com.raja.servingStation.controller.common.BaseController;
import com.raja.servingStation.service.OrderClaimService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class OrderClaimController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(OrderClaimController.class);

    @Autowired
    private ResponseUtil responseUtil;
    @Autowired
    private OrderClaimService orderClaimService;


    /**
     * 提交索赔
     * @param request
     * @return
     */
    @RequestMapping(value = "/order/commitOrderClaim",method = RequestMethod.POST)
    public ResponseEntity logout(HttpServletRequest request){
        try {
            JSONObject parseBody = RequestUtil.parseBody(request);
            checkOrderClaimParam(parseBody);
            ReturnCodes returnCodes = orderClaimService.commitOrderClaim(parseBody);
            if(returnCodes == null)
                return responseUtil.exception(ReturnCodes.ERR_param_ERROR);
            else if(returnCodes.getErrorCode()==1000){
                return responseUtil.success();
            }else
                return responseUtil.exception(returnCodes);

        }catch (BusinessRuntimeException b){
            log.error(b.getMessage(),b);
            return responseUtil.noTokenSuccess(ReturnCodes.ERR_param_ERROR);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return responseUtil.exception(ReturnCodes.REQUEST_EXCEPTION);
        }
    }

    private void checkOrderClaimParam(JSONObject parseBody) throws BusinessRuntimeException {
        Integer orderId = parseBody.getInteger("orderId");
        Integer orderType = parseBody.getInteger("orderType");
        AssertUtils.assertIntegerNotNull(orderId);
        AssertUtils.assertIntegerNotNull(orderType);
    }
}
